import { IDrawerOptions, IModalData, IOverlayContainerOptions } from '@ibiz-template/runtime';
import { OpenViewUtil } from '@ibiz-template/vue3-components';
import {
  createOverlayView,
  getDrawerPlacement,
} from '@ibiz-template/vue3-util';

/**
 * 自定义视图打开方式
 *
 * @export
 * @class CustomOpenViewUtil
 * @extends {OpenViewUtil}
 */
export class CustomOpenViewUtil extends OpenViewUtil {
  /**
   * 打开视图抽屉
   *
   * @param {IParams} [props]
   * @param {IDrawerOptions} [opts]
   * @return {*}  {Promise<IModalData>}
   * @memberof CustomOpenViewUtil
   */
  async openViewDrawer(
    props?: IParams,
    opts?: IDrawerOptions,
  ): Promise<IModalData> {
    const overlay = ibiz.overlay.createDrawer(
      createOverlayView(props),
      props,
      opts,
    );
    await overlay.present();
    const result: IModalData = await overlay.onWillDismiss();
    return result || { ok: false };
  }

  /**
   * 抽屉打开视图
   *
   * @param {string} appViewId
   * @param {IContext} context
   * @param {IParams} [params]
   * @param {IOverlayContainerOptions} [options={}]
   * @return {*}  {Promise<IModalData>}
   * @memberof CustomOpenViewUtil
   */
  async drawer(
    appViewId: string,
    context: IContext,
    params?: IParams,
    options: IOverlayContainerOptions = {},
  ): Promise<IModalData> {
    const appView = await ibiz.hub.config.view.get(appViewId!);
    const placement = getDrawerPlacement(appView.openMode!);

    // 设置默认的modal参数
    const opts = {
      width: appView.width,
      height: appView.height,
      placement,
      ...appView.modalOption,
    };

    return this.openViewDrawer(
      {
        context,
        params,
        viewId: appView.id,
        ctx: options.ctx,
      },
      opts,
    );
  }
}
